package com.kiven.xml.jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 * 第二步：编组（java对象转xml），提供多种编组目的地，以下只介绍两种，一种编组到文件，一种编组到控制台。
 * 
 * @author kiven
 *
 */
public class Object2XmlDemo {
	public static void main(String[] args) {
		Customer customer = new Customer();
		customer.setId(100);
		customer.setName("Test");
		customer.setAge(28);
		
			File file = new File("D:\\test.xml");
			JAXBContext jaxbContext;
			try {
				jaxbContext = JAXBContext.newInstance(Customer.class);
				Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
				jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
				jaxbMarshaller.marshal(customer, file);
				jaxbMarshaller.marshal(customer, System.out);
			} catch (JAXBException e) {
				e.printStackTrace();
			}
	
	}
}
